<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button class="search-btn" to="/search"
      slot="title" type="info" size="small" round icon="search">
        搜索
        </van-button>
    </van-nav-bar>
    <!-- /导航栏 -->

    <!-- 频道列表 -->
    <!--
      通过v-model绑定当前激活标签对应的索引值，默认情况下启用第一个标签
      通过animated属性可以开启切换标签内容时的转场动画
      通过swipeable属性可以开启滑动切换标签页
      通过swipe-threshold属性设置最低滑动标签数
     -->
    <van-tabs v-model="active" animated swipeable class="channel-tabs"
    swipe-threshold="3"
    >
      <van-tab :title="channel.name" v-for="channel in channels" :key="channel.id">
        <!-- 文章列表 -->
        <article-list :channel="channel"></article-list>
      </van-tab>
      <!-- 汉堡栏 -->
      <div slot="nav-right" class="placeholder"></div>
      <div slot="nav-right" class="hamburger-btn" @click="isChennelEditShow = true">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>

    <!-- /频道列表 -->
    <!-- 频道编辑弹出层 -->
    <van-popup
      v-model="isChennelEditShow" close-icon-position="top-left"
      closeable
      position="bottom"
      :style="{ height: '100%' }"
    >
    <!-- 传递channels 传递active高亮索引 定义update-active事件 -->
     <channel-edit :my-channels="channels" :active="active"
     @update-active="onUpdateActive">
     </channel-edit>
    </van-popup>
    <!-- 频道编辑弹出层 -->
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user.js'
import ArticleList from './components/article-list.vue'
import ChannelEdit from './components/channel-edit.vue'
import { mapState } from 'vuex'
import { getItem } from '../../utils/storage.js'
export default {
  name: 'HomeIndex',
  components: {
    ArticleList,
    ChannelEdit
  },
  data () {
    return {
      active: 0,
      channels: [], // 频道列表
      isChennelEditShow: false // 这里我们先设置为 true 就能看到弹窗的页面了
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created () {
    this.loadChannels()
  },
  methods: {
    async loadChannels () {
      try {
        let channels = []
        if (this.user) {
          const { data } = await getUserChannels()
          channels = data.data.channels
        } else {
          // 未登录
          const localChannels = getItem('TOUTIAO_CHANNELS')
          if (localChannels) {
            // 有本地频道数据，则使用
            channels = localChannels
          } else {
            // 没有本地频道数据，则请求获取默认推荐的频道列表
            const { data } = await getUserChannels()
            channels = data.data.channels
          }
        }
        this.channels = channels
      } catch (error) {
        this.$toast('获取频道列表失败')
      }
    },
    // 子组件切换频道
    onUpdateActive (index, isShow = false) {
      this.active = index
      this.isChennelEditShow = isShow
    }
  }
}
</script>

<style lang="less" scoped>
// 注意: 样式不起作用是使用/deep/深度选择符
.home-container {
  padding-top: 174px;
  padding-bottom: 100px;
  :deep(.van-nav-bar__title) {
    // 取消顶部导航的标题插槽宽度限制
    max-width: unset;
  }
  .search-btn{
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    width: 550px;

    .van-icon-search {
      color: aliceblue;
      font-size: 32px;
    }
  }
  :deep(.channel-tabs) {
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      left: 0;
      right: 0;
      z-index: 1;
      height: 82px;
    }
    .van-tab {
      min-width: 200px;
      font-size: 30px;
      border-right: 1px solid #edeff3;
    }

    .van-tabs__line {
      width: 31px;
      height: 6px;
      background-color: #3296fa;
    }

    .placeholder {
      // flex布局时，当某个元素希望不受到影响是，可以设置flex-shrink, 来不参与flex的计算
      flex-shrink: 0;
      height: 88px;
      width: 66px;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      height: 88px;
      width: 66px;
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: #fff;

      // 通过伪类选择器来设置汉堡标签最左侧的竖线
      &::before {
        content: '';
        position: absolute;
        left: 0;
        background-image: url(../../assets/gradient-gray-line.png);
        background-size: contain;
        width: 2px;
        height: 58px;
      }
    }
  }
}
</style>
